package com.cy.store.service.impl;

import com.cy.store.entity.Address;
import com.cy.store.entity.Order;
import com.cy.store.entity.OrderItem;
import com.cy.store.mapper.OrderMapper;
import com.cy.store.service.IAddressService;
import com.cy.store.service.ICartService;
import com.cy.store.service.IOrderService;
import com.cy.store.service.ex.InsertException;
import com.cy.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    ICartService cartService;

    @Autowired
    IAddressService addressService;

    @Autowired
    OrderMapper orderMapper;


    /**
     * 结算
     * @param aid
     * @param uid
     * @param username
     * @param cids
     * @return
     */
    @Override
    public Order create(Integer aid, Integer uid, String username, Integer[] cids) {
        // 获得结算数据
        List<CartVO> list = cartService.getVOByCid(cids, uid);

        // 获得结算总价
        Long totalPrice = 0L;
        for (CartVO l: list){
            totalPrice += l.getRealPrice() * l.getNum();
        }

        // 获得地址信息
        Address address = addressService.getByAid(aid, uid);

        // 创建订单数据对象
        Order order = new Order();

        order.setUid(uid);


        order.setRecvName(address.getName());
        order.setRecvPhone(address.getPhone());
        order.setRecvProvince(address.getProvinceName());
        order.setRecvCity(address.getCityName());
        order.setRecvArea(address.getAreaName());
        order.setRecvAddress(address.getAddress());

        order.setTotalPrice(totalPrice);

        order.setStatus(0);

        order.setOrderTime(new Date());

        // 日志
        order.setCreatedUser(username);
        order.setCreatedTime(new Date());
        order.setModifiedUser(username);
        order.setModifiedTime(new Date());

        // 记录订单数据
        Integer rows = orderMapper.insertOrder(order);
        if(rows != 1){
            throw new InsertException("添加数据时异常");
        }

        // 记录订单中的商品数据
        for(CartVO cart: list){
            OrderItem item = new OrderItem();

            // 补全数据：setOid(order.getOid())
            item.setOid(order.getOid());
            // 补全数据：pid, title, image, price, num
            item.setPid(cart.getPid());
            item.setTitle(cart.getTitle());
            item.setImage(cart.getImage());
            item.setPrice(cart.getRealPrice());
            item.setNum(cart.getNum());
            // 补全数据：4项日志
            item.setCreatedUser(username);
            item.setCreatedTime(new Date());
            item.setModifiedUser(username);
            item.setModifiedTime(new Date());

            // 添加 1行数据
            Integer rows2 = orderMapper.insertOrderItem(item);
            if(rows2 != 1){
                throw new InsertException("添加 1行数据失败");
            }
        }

        return order;
    }
}
